//
//  WBRetweetedView.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
let lightColour = UIColor.init(white: 0.9, alpha: 0.2)

class WBRetweetedView: WBBaseView {

    /// 创建子控件
    /// 转发微博
    let retweetedLabel: UILabel = UILabel(title: "", fontSize: 15)
    
    /// 配图视图
    let retweetedPictureView: WBHomePictureView = WBHomePictureView()
    
    /// viewModel
    var statusViewModel: WBHomeViewModel? {
        didSet{
            //把数据传给配图视图
            retweetedPictureView.statusViewModel = statusViewModel
            
            //设置转发微博的文字
            retweetedLabel.text = statusViewModel?.retweeted_text
            
            //如果有转发微博, 把转发微博的视图调整到正常
            if let _ = statusViewModel?.statusModel.retweeted_status {
                //有转发微博, 则转发微博的文字一定存在, 需要设置文字的顶部间隔
                retweetedLabel.snp.updateConstraints { (make) in
                    make.top.equalTo(statusLayout.margin)
                }
                
                //转发微博有配图
                if let pic_urls = statusViewModel?.statusModel.retweeted_status?.pic_urls, pic_urls.count > 0 {
                    //如果转发微博有配图视图,则设置配图的顶部间距, 高度, 底部间隔
                    retweetedPictureView.snp.updateConstraints({ (make) in
                        make.size.equalTo((statusViewModel?.pictureViewSize)!)
                        make.bottom.equalTo(self).offset(-statusLayout.margin)
                        make.top.equalTo(retweetedLabel.snp.bottom).offset(statusLayout.margin)
                    })
                }
                
                //转发微博没有配图
                //如果转发微博有配图视图,则设置配图的顶部间距, 高度, 底部间隔为0
                else {
                    retweetedPictureView.snp.updateConstraints({ (make) in
                        make.size.equalTo(CGSize.zero)
                        make.bottom.equalTo(self).offset(-statusLayout.margin)
                        make.top.equalTo(retweetedLabel.snp.bottom).offset(0)
                    })
                }
            }
            //如果没有转发微博, 把转发微博调整到看不见
            else {
                //文字的顶部, 设为0
                retweetedLabel.snp.updateConstraints { (make) in
                    make.top.equalTo(self)
                }
                //转发配图的顶部, 高度, 底部设为0
                retweetedPictureView.snp.updateConstraints({ (make) in
                    make.size.equalTo(CGSize.zero)
                    make.bottom.equalTo(self).offset(0)
                    make.top.equalTo(retweetedLabel.snp.bottom).offset(0)
                })
            }
        }
    }
    
    override func wbSetupSubviews() {
        super.wbSetupSubviews()
        setupUI()
    }

}

// MARK: - 设置UI
extension WBRetweetedView {
    /// 设置UI
    func setupUI () {
        //设置背景色
        backgroundColor = lightColour
        
        //添加子视图
        addSubview(retweetedLabel)
        addSubview(retweetedPictureView)
        
        // 自动布局
        retweetedLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-statusLayout.margin)
            make.top.left.equalTo(statusLayout.margin)
        }
        
        retweetedPictureView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.zero)
            make.left.equalTo(self).offset(statusLayout.margin)
            make.bottom.equalTo(self).offset(-statusLayout.margin)
            make.top.equalTo(retweetedLabel.snp.bottom).offset(statusLayout.margin)
        }
    }
}
